package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.NatatoriumListPageEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_ShopAdapter extends RecyclerView.Adapter<ADA_ShopAdapter.ShopViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<NatatoriumListPageEntity.ListBean> mList;
    private OnItemClickListener itemClickListener;

    public ADA_ShopAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    /**
     * 设置数据
     * @param list
     */
    public void setDataList(List<NatatoriumListPageEntity.ListBean> list){
        if (mList != null){
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 更新添加数据
     * @param list
     */
    public void addDataList(List<NatatoriumListPageEntity.ListBean> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ShopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShopViewHolder(mInflater.inflate(R.layout.item_update_shop_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ShopViewHolder holder, int position) {
        final NatatoriumListPageEntity.ListBean listBean = mList.get(position);
        //门店LOGO
        Glide.with(mContext)
                .load(listBean.getLogoUrl())
                .error(R.mipmap.ic_serve_default)
                .fallback(R.mipmap.ic_serve_default)
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .into(holder.ivImgLogoUrl);

        //门店名
        holder.tvShopName.setText(listBean.getName());
        //地址
        holder.tvShopAddress.setText(listBean.getAddress());
        //距离
//        holder.tvShopDistance.setText(listBean.getDistance() + "km");

        //条目点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onClick(listBean.getId(), listBean.getName());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ShopViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img_logoUrl)
        ImageView ivImgLogoUrl;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_shop_address)
        TextView tvShopAddress;
        @BindView(R.id.imageView2)
        ImageView imageView2;
        @BindView(R.id.tv_shop_distance)
        TextView tvShopDistance;
        public ShopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(String shopId, String shopName);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
